package client

import (
	"context"
	"fmt"
	"gitee.com/enjoy_5/goods-rpc/proto/types"
	"gitee.com/xuxinzhi2206/corehub/nacos"
	"google.golang.org/grpc"
)

func clientType(hand func(c types.TypesClient) error) error {
	// 1.连接
	service, err := nacos.FindService(SERVICE, GroupName)
	if err != nil {
		return fmt.Errorf("find server err:%v", err)
	}
	conn, err := grpc.Dial(service, grpc.WithInsecure())
	if err != nil {
		return fmt.Errorf("连接异常:%v", err)
	}
	defer conn.Close()
	c := types.NewTypesClient(conn)
	return hand(c)
}

func GetTypeListByLocation(ctx context.Context, location, ID int64) (info []*types.TypesInfo, err error) {
	err = clientType(func(c types.TypesClient) error {
		res, err := c.GetTypeListByLocation(ctx, &types.GetTypeListByLocationReq{
			ID:       ID,
			Location: location,
		})
		if err != nil {
			return err
		}
		info = res.GetIn()
		return nil
	})
	return
}
